package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.Preject;
import com.itheima.entity.SysUser;
import com.itheima.mapper.SysUserMapper;
import com.itheima.pojo.AccountManagementDto;
import com.itheima.result.PageResult;
import com.itheima.service.AccountManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

@Service
public class AccountManagementServiceImpl implements AccountManagementService {
    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public PageResult list(SysUser dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<AccountManagementDto> page = sysUserMapper.AccountPage(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void batchAdd(List<SysUser> users) {
        for (SysUser user : users) {
            user.setUpdateDate(new Date());
            user.setCreateDate(new Date());
            user.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        }
        sysUserMapper.batchAdd(users);
    }

    @Override
    public void batchDelete(List<Integer> ids) {
        sysUserMapper.batchDelete(ids);
    }

    @Override
    public void updateUser(SysUser sysUser) {
        if (sysUser.getPassword() != null){
            sysUser.setPassword(DigestUtils.md5DigestAsHex(sysUser.getPassword().getBytes()));
        }
        sysUser.setUpdateDate(new Date());
        sysUserMapper.update(sysUser);
    }

    @Override
    public Integer checkResetPassword(Integer userId) {
        SysUser sysUser = sysUserMapper.selectById(userId);
        return sysUser.getIsReset();
    }
}
